﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MJTop.FZhan
{
    public static class FangZhanExt
    {
        /// <summary>
        /// 桌面文件夹名称
        /// </summary>
        internal static string FangZhanDirName = "FZhan";

       

        /// <summary>
        /// 获取请求文件名
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="containExtName">是否包含扩展名</param>
        /// <returns></returns>
        public static string ReqFile(this Uri uri)
        {
            return Path.GetFileName(uri.AbsolutePath);
        }

        /// <summary>
        /// 获取请求目录地址
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static string ReqDir(this Uri uri)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(uri.Scheme + "://" + uri.Authority);
            for (int j = 0; j < uri.Segments.Length - 1; j++)
            {
                sb.Append(uri.Segments[j]);
            }
            return sb.ToString();
        }


        public static Uri CombineUrl(this Uri uri, string reqRelativePath, bool isBaseRawUrl = false)
        {
            string reqPath = reqRelativePath;
            //if (isBaseRawUrl && !reqRelativePath.StartsWith("/"))
            //{
            //    reqPath = "/" + reqRelativePath;
            //}
            return new Uri(uri, reqPath);
        }


        /// <summary>
        /// 获取请求URL的所有目录名，用于创建对应的本地文件夹
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static string LocalRelativeDir(this Uri uri)
        {
            string fileName = uri.ReqFile();
            if (string.IsNullOrWhiteSpace(fileName))
            {
                return string.Empty;
            }
            return uri.AbsolutePath.Replace(uri.ReqFile(), "").Replace("/", "\\").TrimEnd('\\');
        }


        /// <summary>
        /// 当前url对应的绝对路径 的目录地址
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static string LocalAbsDir(this Uri uri)
        {
            string dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), FangZhanDirName);
           
            string absDir = Path.Combine(dir, uri.Host) + LocalRelativeDir(uri);

            if (!Directory.Exists(absDir))
            {
                Directory.CreateDirectory(absDir);
            }
            return absDir;
        }


        public static string LocalAbsPath(this Uri uri)
        {
            string dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), FangZhanDirName);

            string absPath = Path.Combine(dir, uri.Host) + LocalRelativeDir(uri);

            if (!Directory.Exists(absPath))
            {
                Directory.CreateDirectory(absPath);
            }
            absPath = Path.Combine(absPath, uri.ReqFile());
            return absPath;
        }


        /// <summary>
        /// 获取字符串的MD5码
        /// </summary>
        public static string GetMD5Hash(this string input)
        {
            MD5 md5 = MD5.Create();
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            byte[] hashBytes = md5.ComputeHash(inputBytes);            
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2"));
            }
            return sb.ToString();
        }
    }
}
